package com.hsy.swu.clouddb

import android.content.Context
import android.content.Intent
import androidx.room.Room
import com.hsy.swu.hsyutils.HsyActivity
import com.hsy.swu.hsyutils.MLog
import com.hsy.swu.hsyutils.spRead
import com.hsy.swu.hsyutils.spWrite
import com.hsy.swu.login.LoginActivity
import com.hsy.swu.roomdb.AppDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject

class MyUser(
    var objectId: String,
    var account: String,
    var password: String,
    var nickName: String = "",
    var createdAt: String = "",
    var updatedAt: String = "",
) {
    companion object {
        const val USER_INFO = "userInfo"
        val currentUser: MyUser = MyUser("", "", "")

        /** 退出登录，清除账号密码文件内容 */
        suspend fun logOut(activity: HsyActivity<*, *>) {
            withContext(Dispatchers.IO) {
                val appDatabase = Room.databaseBuilder(
                    activity,
                    AppDatabase::class.java,
                    "swu"
                ).build()
                appDatabase.apply {
                    courseDao().deleteAllByAccount(currentUser.account) // 删除课表信息
                    courseDao().deleteAllCustomCourse()
                    gradeDao().deleteAll() // 删除成绩信息
                    textbookDao().deleteAll() // 删除教材信息
                    examDao().deleteAll() // 删除考试信息
                }
            }
            activity.spWrite("", USER_INFO)
            currentUser.apply {
                objectId = ""
                account = ""
                password = ""
                nickName = ""
            }
            activity.startActivity(Intent(activity, LoginActivity::class.java))
            activity.finish()
        }
    }

    fun readFirst4FromSp(context: Context, fileName: String) {
        val userInfoJsonString = context.spRead(fileName)
        MLog.v("parseUserJsonString -> $userInfoJsonString")
        if (userInfoJsonString != "") {
            val jsonObject = JSONObject(userInfoJsonString)
            objectId = jsonObject.getString("objectId").toString()
            account = jsonObject.getString("account").toString()
            password = jsonObject.getString("password").toString()
            nickName = jsonObject.getString("nickName").toString()
            MLog.v("jsonObj -> $jsonObject")
        }
    }

    // 把前四个属性序列化
    fun writeFirst4ToSp(context: Context, fileName: String) {
        context.spWrite(
            "{\"objectId\":\"${objectId}\"," +
                    "\"account\":\"${account}\"," +
                    "\"password\":\"${password}\"," +
                    "\"nickName\":\"${nickName}\"}",
            fileName
        )
    }

    override fun toString(): String {
        return "{\n" +
                "   objectId    ->  $objectId \n" +
                "   account     ->  $account \n" +
                "   password    ->  $password \n" +
                "   nickName    ->  $nickName \n" +
                "   createdAt   ->  $createdAt \n" +
                "   updatedAt   ->  $updatedAt \n" +
                "}"
    }
}